#include <stdio.h> 
#include "USER_CFG.h" 
#include <stdlib.h> 
#include <string.h> 
 
 
unsigned char G_FlashBuf[2048]; 
 
unsigned int GUI_LCM_XMAX,GUI_LCM_YMAX,LCD_CLK; 
 
REGION G_REG_DIR; 
REGION G_REG_FLR; 
REGION G_REG_TIME; 
REGION G_REG_DAY; 
REGION G_REG_WEEK; 
REGION G_REG_ALARM; 
REGION G_REG_BKGND; 
REGION G_REG_LOGO; 
REGION G_REG_OTHER; 
REGION G_REG_VIDEO; 
REGION G_REG_TXT; 
unsigned int BKGND_wait; 
unsigned int BKGND_effect; 
REGION G_REG_DIR_T2; 
REGION G_REG_FLR_T2; 
REGION G_REG_ALARM_T2; 
 
REGION REG_DES; 
 
unsigned int STEP_FLOOR_MOVE; 
unsigned int Flag_Lift2_Exist;

unsigned int G_LFTTXT_txt_vfmode; 
unsigned int G_LFTTXT_vf_speed; 
unsigned int G_LFTTXT_txt_color;
unsigned int G_LFTTXT_font_id; 
 
extern volatile unsigned int LIFT_FLAG; 
extern volatile unsigned int Flag_CN; 
 
/********************************************************************************************************* 
 œ«±äÁ¿ÃûÓë×Ö•û±êÇ©ÁŽœÓÆðÀŽ œ«±äÁ¿Ãû<->×Ö•ûlink 
*********************************************************************************************************/ 
const _struct_link_var C_str2var[] = { 
  {Tags_LCD_XMAX,       &GUI_LCM_XMAX}, 
  {Tags_LCD_YMAX,       &GUI_LCM_YMAX}, 
  {Tags_LCD_CLK,        &LCD_CLK}, 
  {Tags_REG_DIR_SX,     &G_REG_DIR.s_x}, 
  {Tags_REG_DIR_SY,     &G_REG_DIR.s_y}, 
  {Tags_REG_DIR_XLEN,   &G_REG_DIR.x_len}, 
  {Tags_REG_DIR_YLEN,   &G_REG_DIR.y_len}, 
  {Tags_REG_FLR_SX,     &G_REG_FLR.s_x}, 
  {Tags_REG_FLR_SY,     &G_REG_FLR.s_y}, 
  {Tags_REG_FLR_XLEN,   &G_REG_FLR.x_len}, 
  {Tags_REG_FLR_YLEN,   &G_REG_FLR.y_len}, 
  {Tags_REG_TIME_SX,    &G_REG_TIME.s_x}, 
  {Tags_REG_TIME_SY,    &G_REG_TIME.s_y}, 
  {Tags_REG_TIME_XLEN,  &G_REG_TIME.x_len}, 
  {Tags_REG_TIME_YLEN,  &G_REG_TIME.y_len}, 
  {Tags_REG_DAY_SX,    &G_REG_DAY.s_x}, 
  {Tags_REG_DAY_SY,    &G_REG_DAY.s_y}, 
  {Tags_REG_DAY_XLEN,  &G_REG_DAY.x_len}, 
  {Tags_REG_DAY_YLEN,  &G_REG_DAY.y_len}, 
  {Tags_REG_WEEK_SX,    &G_REG_WEEK.s_x}, 
  {Tags_REG_WEEK_SY,    &G_REG_WEEK.s_y}, 
  {Tags_REG_WEEK_XLEN,  &G_REG_WEEK.x_len}, 
  {Tags_REG_WEEK_YLEN,  &G_REG_WEEK.y_len}, 
  {Tags_REG_ALARM_SX,   &G_REG_ALARM.s_x}, 
  {Tags_REG_ALARM_SY,   &G_REG_ALARM.s_y}, 
  {Tags_REG_ALARM_XLEN, &G_REG_ALARM.x_len}, 
  {Tags_REG_ALARM_YLEN, &G_REG_ALARM.y_len}, 
  {Tags_REG_BKGND_SX,   &G_REG_BKGND.s_x}, 
  {Tags_REG_BKGND_SY,   &G_REG_BKGND.s_y}, 
  {Tags_REG_BKGND_XLEN, &G_REG_BKGND.x_len}, 
  {Tags_REG_BKGND_YLEN, &G_REG_BKGND.y_len}, 
  {Tags_REG_LOGO_SX,    &G_REG_LOGO.s_x}, 
  {Tags_REG_LOGO_SY,    &G_REG_LOGO.s_y}, 
  {Tags_REG_LOGO_XLEN,  &G_REG_LOGO.x_len}, 
  {Tags_REG_LOGO_YLEN,  &G_REG_LOGO.y_len}, 
  {Tags_REG_OTHER_SX,   &G_REG_OTHER.s_x}, 
  {Tags_REG_OTHER_SY,   &G_REG_OTHER.s_y}, 
  {Tags_REG_OTHER_XLEN, &G_REG_OTHER.x_len}, 
  {Tags_REG_OTHER_YLEN, &G_REG_OTHER.y_len}, 
  {Tags_REG_VIDEO_SX,   &G_REG_VIDEO.s_x}, 
  {Tags_REG_VIDEO_SY,   &G_REG_VIDEO.s_y}, 
  {Tags_REG_VIDEO_XLEN, &G_REG_VIDEO.x_len}, 
  {Tags_REG_VIDEO_YLEN, &G_REG_VIDEO.y_len}, 
  {Tags_REG_TXT_SX,     &G_REG_TXT.s_x}, 
  {Tags_REG_TXT_SY,     &G_REG_TXT.s_y}, 
  {Tags_REG_TXT_XLEN,   &G_REG_TXT.x_len}, 
  {Tags_REG_TXT_YLEN,   &G_REG_TXT.y_len}, 
  {Tags_REG_TXT_MODE,    (unsigned int *)&G_LFTTXT_txt_vfmode}, 
  {Tags_REG_TXT_SPEED,   (unsigned int *)&G_LFTTXT_vf_speed}, 
  {Tags_REG_TXT_COLOR,   (unsigned int *)&G_LFTTXT_txt_color}, 
  {Tags_REG_TXT_FONT,    &G_LFTTXT_font_id}, 
  
  {Tags_REG_DIR_T2_SX,     &G_REG_DIR_T2.s_x}, 
  {Tags_REG_DIR_T2_SY,     &G_REG_DIR_T2.s_y}, 
  {Tags_REG_DIR_T2_XLEN,   &G_REG_DIR_T2.x_len}, 
  {Tags_REG_DIR_T2_YLEN,   &G_REG_DIR_T2.y_len}, 
  {Tags_REG_FLR_T2_SX,     &G_REG_FLR_T2.s_x}, 
  {Tags_REG_FLR_T2_SY,     &G_REG_FLR_T2.s_y}, 
  {Tags_REG_FLR_T2_XLEN,   &G_REG_FLR_T2.x_len}, 
  {Tags_REG_FLR_T2_YLEN,   &G_REG_FLR_T2.y_len}, 
  {Tags_REG_ALARM_T2_SX,   &G_REG_ALARM_T2.s_x}, 
  {Tags_REG_ALARM_T2_SY,   &G_REG_ALARM_T2.s_y}, 
  {Tags_REG_ALARM_T2_XLEN, &G_REG_ALARM_T2.x_len}, 
  {Tags_REG_ALARM_T2_YLEN, &G_REG_ALARM_T2.y_len}, 
  {Tags_REG_BKGND_WTIME, &BKGND_wait}, 
  {Tags_REG_BKGND_EFFECT,&BKGND_effect}, 
 
//ÏÔÊŸÄ¿µÄ²ãÇøÓò 
  {Tags_REG_DES_SX,   &REG_DES.s_x}, 
  {Tags_REG_DES_SY,   &REG_DES.s_y}, 
  {Tags_REG_DES_XLEN, &REG_DES.x_len}, 
  {Tags_REG_DES_YLEN, &REG_DES.y_len}, 
 
  //Â¥²ã¹ö¶¯ËÙ¶È 
  {Tags_REG_FLOOR_SPEED,&STEP_FLOOR_MOVE}, 
}; 
 
/********************************************************************************************************* 
 ÅäÖÃÎÄŒþµÄÎÄŒþÃûÁÐ±í 
*********************************************************************************************************/ 
const char *fn_csv[] = 
{ 
	"/mnt/nand1-2/LIFT/LIFT_0C.csv", 
	"/mnt/nand1-2/LIFT/LIFT_90C.csv", 
	"/mnt/nand1-2/LIFT/LIFT_180C.csv", 
	"/mnt/nand1-2/LIFT/LIFT_270C.csv" 
}; 
 
const char *fn_csv_2[] = 
{ 
	"2:/LIFT/LIFT_0C.csv", 
	"2:/LIFT/LIFT_90C.csv", 
	"2:/LIFT/LIFT_180C.csv", 
	"2:/LIFT/LIFT_270C.csv" 
}; 
 
/****************************************************** 
 * ÔÚ¶ÁÈ¡ÅäÖÃÎÄŒþÊ§°ÜÊ±µÄÄ¬ÈÏ³õÊŒ»¯²ÎÊý 
******************************************************/ 
void ConfigDefault(void) 
{ 
 
} 
 

/****************************************************** 
 * Function name:	CheckFileExist
 * Descriptions:        Check file is exist or not
******************************************************/ 
unsigned char CheckFileExist0(char * filename) 
{ 
	FILE* ckfp; 
	ckfp = fopen(filename,"r"); 
 
	if(ckfp != NULL) 
	{ 
		fclose(ckfp); 
		return 1; 
	} 
	else 
	{					 
		fclose(ckfp); 
		return 0; 
	} 
} 
 
/********************************************************************************************************* 
** Function name:           Nand_GetCsv 
** Descriptions:            ŽÓnand»ñÈ¡csvÅäÖÃÎÄŒþ 
*********************************************************************************************************/ 
unsigned char GetCsv(const char *fn,const _struct_link_var *plink_var) 
{ 
	FILE* 	fresult; 
	int readlen; 
	unsigned int brw; 
	char *pcsv_head = NULL; 
	char *pcsv_end = NULL; 
	unsigned char i; 
	char var_int[10]; 
	printf("GetCsv start\n"); 
 
	fresult = fopen(fn, "r"); 
	if(NULL==fresult) 
		return FALSE; 
	readlen = fread(G_FlashBuf, 1, sizeof(G_FlashBuf) , fresult); 
	if(0==readlen) 
		return FALSE; 
	fclose(fresult); 
	if(strstr((char *)G_FlashBuf,Tags_FILE_HEAD)==NULL) 
		return FALSE; 
	  for(i=0;i<sizeof(C_str2var)/sizeof(_struct_link_var);i++) 
	  { 
	    pcsv_head = strstr((char *)G_FlashBuf,C_str2var[i].plk_var); 
	    if(pcsv_head == NULL) // ÎŽŒìË÷µœžÃ±äÁ¿ 
//	      //return FALSE; 
	      continue; 
	    pcsv_end = strstr(pcsv_head,Tags_DOT); 
	    if(pcsv_end == NULL) 
	      return FALSE; 
	    if((pcsv_end-pcsv_head)>strlen(C_str2var[i].plk_var)) // ±äÁ¿µÄÊäÈëŽíÎó£¬ŽæÔÚ²»•ûºÏ¹æÔòµÄ±äÁ¿ 
	      return FALSE; 
	    pcsv_head = pcsv_end+sizeof(char); 
	    pcsv_end  = strstr(pcsv_head,Tags_LINE_END); 
	    if(pcsv_end == NULL) 
	      return FALSE; 
	    if((pcsv_end-pcsv_head)>8) // ±äÁ¿ÖµŽóÓÚ5Î»£¬ŒŽ³¬¹ý99999999 
	      return FALSE; 
	    memcpy(var_int,pcsv_head,(pcsv_end-pcsv_head)/sizeof(char)); 
	    var_int[(pcsv_end-pcsv_head)/sizeof(char)] = '\0'; // end char 
//	    printf("%s : ",C_str2var[i].plk_var); 
//	    printf("%s\r\n",var_int); 
	    *C_str2var[i].pvar = atoi(var_int); // ¿œ±Ž±äÁ¿Öµ 
	  } 
	  return TRUE; 
} 
 
/********************************************************************************************************* 
** Function name:           GetCsv2Cfg 
** Descriptions:            œ«CSVÎÄŒþµÄÄÚÈÝÅäÖÃµœœçÃæ²ÎÊý±äÁ¿ 
** input parameters:        dmode£ºÎªÏµÍ³µ±Ç°µÄÏÔÊŸ²ÎÊý 
** Output parameters:       TRUE/FALSE 
** Returned value:          NONE 
*********************************************************************************************************/ 
unsigned char GetCsv2Cfg(_enum_DMODE dmode) 
{ 
	REGION region[16]; 
	unsigned int i; 
 
	for(i=0;i<16;i++) 
	{ 
		region[i].s_x = 0; 
		region[i].s_y = 0; 
		region[i].x_len = 0; 
		region[i].y_len = 0; 
	} 
 
	G_REG_DIR = region[0]; 
	G_REG_FLR = region[1]; 
	G_REG_TIME = region[2]; 
	G_REG_DAY = region[3]; 
	G_REG_WEEK = region[4]; 
	G_REG_ALARM = region[5]; 
	G_REG_BKGND = region[6]; 
	G_REG_LOGO = region[7]; 
	G_REG_OTHER = region[8]; 
	G_REG_VIDEO = region[9]; 
	G_REG_TXT = region[10]; 
	G_REG_DIR_T2 = region[11]; 
	G_REG_FLR_T2 = region[12]; 
	G_REG_ALARM_T2 = region[13]; 
 
	REG_DES = region[14]; 
 
	if((LIFT_FLAG == 0) || (LIFT_FLAG == 3)) 
	{ 
		if(GetCsv(fn_csv[dmode],C_str2var)==FALSE) 
		{ 
			return FALSE; 
		} 
		else 
		{ 
			return TRUE; 
		} 
	} 
	else 
	{ 
		if(GetCsv(fn_csv_2[dmode],C_str2var)==FALSE) 
		{ 
			return FALSE; 
		} 
		else 
		{ 
			return TRUE; 
		} 
	} 
} 
 
/********************************************************************************************************* 
** Function name:           Nand2GetSysCfg 
** Descriptions:            »ñÈ¡ÏµÍ³ÅäÖÃÎÄŒþ 
** input parameters:        none 
** Output parameters:       none 
** Returned value:          •µ»Ø¶ÔÓŠµÄÅäÖÃœÇ¶È 
*********************************************************************************************************/ 
_enum_DMODE GetSysCfg(void) 
{
	if(GetCsv2Cfg(dmode_0C)==TRUE) 
	    return dmode_0C; 
	if(GetCsv2Cfg(dmode_90C)==TRUE) 
	    return dmode_90C; 
	if(GetCsv2Cfg(dmode_180C)==TRUE) 
	    return dmode_180C; 
	if(GetCsv2Cfg(dmode_270C)==TRUE) 
	    return dmode_270C;
	 
	ConfigDefault(); 
	 
	return dmode_error; 
} 
 
 
/********************************************************************************************************* 
  END FILE  
*********************************************************************************************************/
